﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ReaderLendBook : System.Web.UI.Page
{
    string readerID;
    string bookID;
    public string tips;
    string bookname;
    protected void Page_Load(object sender, EventArgs e)
    {
        


    }
    protected void sureBorrowAgain_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in mybookdata.Rows)
        {
            Control ctrl = row.FindControl("CheckBox");

            if ((ctrl as CheckBox).Checked)
            {
                TableCellCollection cell = row.Cells;
                string bookID = cell[1].Text;
                SQL sql = new SQL();
                string readerID = sql.getDataFormTable("readerID", "Reader_info", "ReaderName", Session["userName"].ToString(), "", "", "", "", 1);
                bool flag = sql.BorrowAgain(readerID, bookID);
                if (flag)
                {
                    Response.Write("<script language=javascript>alert('续借成功');window.location = 'UserIndex.aspx';</script>");

                }
                else
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('您已经达到了最大续借次数')", true);

            }

        }



    }
    protected void Cancel_Click(object sender, EventArgs e)
    {

    }
    protected void chaxun_Click(object sender, EventArgs e)
    {
        bookID = TBbookid.Text;
        readerID = TextBox1.Text;
        SqlDataSource1.SelectCommand = "select bookid as 图书编号,bookname as 图书名称,writer as 作者 , Price as 价格,Publish_company.PublishCompanyName as 出版社, CurrentNum as 目前在库 ,ReaderID as 读者编号,ReaderName as 读者姓名 from Book_info,Publish_company ,Reader_info where Publish_company.PublishCompanyID=Book_info.PublishCompanyID and BookID='"+bookID+"' and reader_info.readerid='"+readerID+"'";
        //SqlDataSource1.SelectParameters["name"].DefaultValue = readerID;



    }
    protected void sureBorrow_Click(object sender, EventArgs e)
    {
        SQL sql = new SQL();
        bookID = TBbookid.Text;
        readerID = TextBox1.Text;
        bookname = sql.getDataFormTable("bookname", "book_info", "bookID", bookID, "", "", "", "", 1);
        int booknum = Convert.ToInt32(sql.getDataFormTable("CURRENTNUM", "book_info", "bookID", bookID, "", "", "", "", 1));
        int BeborrowTimes = Convert.ToInt32(sql.getDataFormTable("BeBorrowTimes", "book_info", "bookID", bookID, "", "", "", "", 1));
        //执行步骤：
        //1、检查用户是否登陆
        //2、检查密码是否正确
        //3、检查图书是否在库
        //4、检查图书剩余数量
        //5、借阅单号写入借阅单表
        //6\检查是否超过自己读者类型的最大限制本数量
        //6、图书信息表修改目前图书数量，图书状态，图书被借次数

        string status = sql.getDataFormTable("statusID", "book_info", "bookID", bookID, "", "", "", "", 1);
        if (status.Equals("1"))
        {
            int kejieyuetushu = 0;
            string typeid = sql.getDataFormTable("ReaderType", "Reader_info", "readerid", readerID, "", "", "", "", 1);
            kejieyuetushu = Convert.ToInt32(sql.EXQSQL("select maxlend from reader_type where readertype=" + typeid + ""));
            if (Convert.ToInt32(sql.EXQSQL("select count(*) from Borrow_books_manage where readerID='" + readerID + "' and returndate is null ")) <= kejieyuetushu)
            {
                if (Convert.ToDouble(sql.EXQSQL("select LOWERLIMIT FROM System_state WHERE INFOID=1")) < Convert.ToDouble(sql.EXQSQL("select account from Reader_info where readerid='" + Session["userName"].ToString() + "'")))
                {
                    if (Convert.ToInt32(sql.getDataFormTable("CURRENTNUM", "book_info", "bookID", bookID, "", "", "", "", 1)) > 0)
                    {
                        string shouldReturnday = DateTime.Now.AddDays(Convert.ToInt32(sql.EXQSQL("select Borrowdays from System_state"))).ToString();
                        sql.updateBOOKinfo("insert into Borrow_books_manage(BookID,ReaderID,DDNUM,LendBookDate,ShouldReturnDate,XUJIETIMES) values('" + bookID + "','" + readerID + "','" + bookID + readerID + string.Format("{0:yyyymmddhhmmss}", DateTime.Now) + "','" + DateTime.Now.ToString() + "','" + shouldReturnday + "','0')");//写入借阅单表

                        sql.EXQSQL("update Book_info set CurrentNum=" + (booknum - 1) + ",statusid=2,BeBorrowTimes=" + (BeborrowTimes + 1) + " where bookid=" + bookID + "");
                        tips = "<div class=\"alert alert-success\"><strong>借阅成功!</strong> 读者  " + readerID + "   成功借阅图书  《" + bookname + " 》。</div>";

                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('图书已经全部借出');</script>");
                        tips = " <div class=\"alert alert-danger\" style=\"margin-top:20px;\"><strong>错误：图书《" + bookname + "》已全部借出。</strong></div></div>";

                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('读者余额不足');</script>");
                }

            }
            else
            {
                Response.Write("<script language=javascript>alert('您已经超过最大可借数量');</script>");
                tips = " <div class=\"alert alert-danger\" style=\"margin-top:20px;\"><strong>错误：读者 " + readerID + " 已经超过最大可借阅书籍数量</strong></div></div>";


            }

        }
        else
        {
            Response.Write("<script language=javascript>alert('图书不在库');</script>");
            tips = " <div class=\"alert alert-danger\" style=\"margin-top:20px;\"><strong>错误：图书《" + bookname + "》不在库</strong></div></div>";
        }

    }
}